<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部 -->
      <el-cart class="tree-card">
        <TreeTools :tree-node="company" :is-root="true" @adddepts="adddepts" />
      </el-cart>
      <!-- 树形结构 -->
      <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
        <TreeTools slot-scope="{ data }" :tree-node="data" @delDept="getDepartment" @adddepts="adddepts" @editdepts="editdepts" />
      </el-tree>
      <AddDept ref="addEdit" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartment" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Departments',
  components: { TreeTools, AddDept },
  data() {
    return {
      company: { name: '某某科技股份有限公司', manager: '负责人', id: '' },
      departs: [{ name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false, // 显示窗体
      node: null // 点击的当前节点
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const { depts } = await getDepartment()
      console.log(depts)
      this.departs = tranListToTreeData(depts, '')
    },
    // 新增
    adddepts(node) {
      this.showDialog = true
      this.node = node
    },
    // 编辑
    editdepts(node) {
      this.showDialog = true
      this.node = node
      // 调用部门详情 详情方法在子组件
      this.$refs.addEdit.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card{
  font-size:20px;
  .header{
    height: 40px;
    border-bottom:1px solid #ccc;
    margin-bottom: 20px;
  }
}
</style>
